package org.zjx.action.game.gather;

import org.zjx.analysis.window.InventorAnalysis;
import org.zjx.action.game.AbstractWindowAction;
import org.zjx.action.game.inventor.InventorDescriptionAction;
import org.zjx.action.game.inventor.InventorHandOffAction;
import org.zjx.action.game.inventor.MatchInventorAction;
import org.zjx.core.IWindow;
import org.zjx.state.ItemState;

import java.awt.*;
import java.awt.event.KeyEvent;

public class GatherPropAction extends AbstractWindowAction {
    private InventorAnalysis inventorAnalysis;

    public GatherPropAction(IWindow targetWindow) {
        super(targetWindow);
    }

    @Override
    public void open() {
        analysis();
        mouseAloofWindow(inventorAnalysis.windowArea == null ? inventorAnalysis.getOriginBounds() : inventorAnalysis.windowArea);
        if (inventorAnalysis.isOpened()) return;
        hotkey(KeyEvent.VK_ALT, KeyEvent.VK_E);
        flush();
        open();
    }

    @Override
    public void close() {
        analysis();
        if (!inventorAnalysis.isOpened()) return;
        hotkey(KeyEvent.VK_ALT, KeyEvent.VK_E);
        flush();
        close();
    }

    @Override
    public void reSetWindow() {
        if (inventorAnalysis.isTransborder()) {
            drag(inventorAnalysis.conversion(inventorAnalysis.windowArea,inventorAnalysis.getRefImageArea()), inventorAnalysis.conversion(inventorAnalysis.getOriginBounds(),inventorAnalysis.getRefImageArea()));
            analysis();
        }
    }

    public void mouseAloofWindow(Rectangle rect) {
        if (inventorAnalysis.mouseInRect(rect)) {
            Point safePoint = inventorAnalysis.findLargestRemainingAreaCenter(rect);
            move(safePoint);
        }
    }

    @Override
    protected void doExecute() throws Exception {
        if (stateManager.getGameState().isGatherProp()) return;
        open();
        reSetWindow();
        stateManager.registerState(new ItemState());
        new MatchInventorAction(targetWindow, inventorAnalysis.getItems()).execute();
        gatherDescriptions();
//        handOff();
//        waitPane();
//        analysis();
//        new MatchInventorAction(targetWindow, inventorAnalysis.getItems()).execute();
//        gatherDescriptions();
//        handOff();
        close();
        stateManager.getGameState().setGatherProp(true);
    }

    private void analysis() {
        inventorAnalysis = new InventorAnalysis(targetWindow, stateManager.getGameState().getScreenshot());
    }

    //采集/同步 道具栏道具信息
    private void gatherDescriptions() {
        InventorDescriptionAction inventorDescriptionAction = new InventorDescriptionAction(targetWindow, inventorAnalysis);
        inventorDescriptionAction.execute();
    }

    //切换道具栏
    private void handOff() {
        InventorHandOffAction inventorHandOffAction = new InventorHandOffAction(targetWindow, inventorAnalysis);
        inventorHandOffAction.execute();
    }

    //等待提示结束
    private void waitPane() {
        delay(4000);
    }
}
